<?
include("session.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../webkit/utils/jquery.js"></script>
<script type="text/javascript" src="../webkit/grid/import.js"></script>
<script type="text/javascript" src="../webkit/dialog/import.js"></script>
<script type="text/javascript" src="./common.js"></script>
<link type="text/css" rel="stylesheet" href="page.css"/>
</head>
<body>


<table width=100% height=100% cellspacing=0 cellpadding=0>
	<tr bgcolor="#eeeeee">
	<form name=f>
	<td height=30>&nbsp;
	任务状态：<select id="task_status"><option value=''>-请选择-</option><option value='0'>未启动</option><option value='1'>执行中</option><option value='2'>暂停中</option><option value='3'>已结束</option></select>　
	任务名称：<input type=text id='task_name' size=12>　
	外呼号码：<input type=text id='callout_number' size=12>　
	<?if($_SESSION['user_flag'] == "admin") {?>
	创建用户：<input type=text id='user_name' size=12>　
	<?}?>
	<input type="button" value="查询" onclick="search()">　
	</td>
	</tr>
	<tr>
	<td>
		<table id="flex1" style="display:none"></table>
	</td>
	</form>
	</tr>
</table>


<script type="text/javascript">
	var h = parent.document.body.clientHeight-(115+82);
	if(h < 0) h = 'auto'
	$("#flex1").flexigrid({
		url: 'task_query.php?action=list',
		dataType: 'json',
		colModel : [
			{display: 'ID', name : 'a', width : 30, sortable : false, align: 'center'},
			{display: '任务名称', name : 'a', width : 200, sortable : false, align: 'center'},
			{display: '外呼号码', name : 'callout_number', width : 100, sortable : true, align: 'center'},
			{display: '外呼类型', name : 'call_type', width : 80, sortable : false, align: 'center'},
			{display: '语音内容', name : 'voice_content', width : 120, sortable : false, align: 'center'},
			{display: '号码总数', name : 'call_count', width : 50, sortable : false, align: 'center'},
			{display: '成功条数', name : 'success_count', width : 50, sortable : false, align: 'center'},
			{display: '失败条数', name : 'fail_count', width : 50, sortable : false, align: 'center'},
			{display: '创建时间', name : 'create_time', width : 120, sortable : true, align: 'center'},
			{display: '开始时间', name : 'start_time', width : 120, sortable : true, align: 'center'},
			{display: '任务状态', name : 'task_status', width : 100, sortable : false, align: 'center'},
			{display: '创建用户', name : 'user_name', width : 80, sortable : false, align: 'center'},
			{display: '任务备注', name : 'task_desc', width : 80, sortable : false, align: 'center'}
			],
		buttons : [
            {name: '新增', bclass: 'add', onpress : add_task},     
			{name: '修改', bclass: 'edit', onpress : edit},     
			<?if($_SESSION['user_flag'] == "admin"){?>{name: '删除', bclass: 'del', onpress : del},<?}?>
			{separator: true},
			{name: '导入号码', bclass: 'tool', onpress : do_import},
			{separator: true},
			{name: '启动任务', bclass: 'start', onpress : start_task},
			{name: '结束任务', bclass: 'del', onpress : stop_task},
			{separator: true},
			{name: '用户按键统计', bclass: 'other', onpress : select_key_report},
			{name: '失败原因统计', bclass: 'other', onpress : failed_call_report},
			{name: '时段外呼统计', bclass: 'other', onpress : day_time_report}
		],
		async : true,
		usepager: true,
		title: '任务管理',
		useRp: true,
		rp: 15,
		showTableToggleBtn: false,
		width: 'auto',
		height: h,
		resizable : false,
		singleSelect : true,
		onSuccess : function() { show_sum_total("6,5,7,") }	
	});

    
    $('#flex1').dblclick( function (e) { 
        select_row_on_dblclick(e);
        edit();
    }); 
   
	<?
	$add_url = "task_panel.php?op=add";
	$edit_url = "task_panel.php?op=edit&id=";
	$del_url = "task_query.php?action=del&id=";
	$dlg_size = "600,380";
	require("grid_common_js.php");
	?>

	function add_task() {
		/*$.get("task_query.php?action=get_active_task_count", function(n){
			if(parseInt(n) > 1) {
				alert("当前已存在一个尚未完成的任务，不能创建新任务！");
			} else {
				add(); 
			}
		});*/
		add();
	}

	function do_import() {
		var id = $("#flex1").getSelectedRowId();
		if(isNaN(id) || id<1) {
			alert("请选择一条有效记录！");
			return false;
		}
		dlg = new Dialog({ title: '导入号码', resizable:false, maximizable:false}).href("call_import.php?task_id="+id);
		dlg.show().setSize(520, 230).center();
	}

	function show_voice(v) {
		dlg = new Dialog({ title: '查看录音', resizable:false, maximizable:false}).href("<?echo $_hncc_interface_uri?>/play_voice.jsp?voice_file="+v);
		dlg.show().setSize(520, 130).center();
	}

	function start_task() {
		var id = $("#flex1").getSelectedRowId();
		if(isNaN(id) || id<1) {
			alert("请选择一条有效记录！");
			return false;
		}
		if(confirm('确定要启动此外呼任务吗？')) {
			$.get("task_query.php?action=start&id="+id, function(data) {
				if(data == "ok") {
					alert("任务已成功启动！");
					refreshGrid();
				} else if(data == "this_task_is_running"){
					alert("此任务已经处于运行中，不必重复操作！");
				} else if(data == "task_is_running_over_max"){
					alert("操作失败，普通用户最多同时启动10个任务！");
				} else if(data == "another_task_is_running"){
					alert("当前已经有另一个任务处于运行中，不能同时启动两个任务！");
				} else {
					alert(data);
				}
			});
		}
	}

	function stop_task() {
		var id = $("#flex1").getSelectedRowId();
		if(isNaN(id) || id<1) {
			alert("请选择一条有效记录！");
			return false;
		}
		if(confirm('确定要结束此外呼任务吗？')) {
			$.get("task_query.php?action=stop&id="+id, function(data) {
				if(data == "ok") {
					alert("任务已成功结束！");
					refreshGrid();
				} else {
					alert(data);
				}
			});
		}
	}

	function pause_task(id) {
		if(confirm('确定要暂停此外呼任务吗？')) {
			$.get("task_query.php?action=set_pause&id="+id, function(data) {
				if(data == "ok") {
					alert("任务已暂停执行！");
					refreshGrid();
				} else {
					alert(data);
				}
			});
		}
	}

	function continue_task(id) {
		if(confirm('确定要恢复此外呼任务吗？')) {
			$.get("task_query.php?action=set_continue&id="+id, function(data) {
				if(data == "ok") {
					alert("任务已恢复执行！");
					refreshGrid();
				} else {
					alert(data);
				}
			});
		}
	}


	function select_key_report() {
		var id = $("#flex1").getSelectedRowId();
		if(isNaN(id) || id<1) {
			alert("请选择一条有效记录！");
			return false;
		}
		dlg = new Dialog({ title: '用户按键统计', resizable:false, maximizable:false}).href("select_key_report.php?task_id="+id);
		dlg.show().setSize(600, 400).center();
	}


	function failed_call_report() {
		var id = $("#flex1").getSelectedRowId();
		if(isNaN(id) || id<1) {
			alert("请选择一条有效记录！");
			return false;
		}
		dlg = new Dialog({ title: '失败原因统计', resizable:false, maximizable:false}).href("failed_call_report.php?task_id="+id);
		dlg.show().setSize(600, 400).center();
	}


	function day_time_report() {
		var id = $("#flex1").getSelectedRowId();
		if(isNaN(id) || id<1) {
			id = ""
		}
		dlg = new Dialog({ title: '时段外呼统计', resizable:false, maximizable:false}).href("day_time_report.php?task_id="+id);
		dlg.show().setSize(800, 400).center();
	}



	function search() { 
		var url = "task_query.php?action=list";
        if($("#task_status").val()!="") {
            url += "&task_status="+ $("#task_status").val();
        }      
        if($("#callout_number").val()!="") {
            url += "&callout_number="+ $("#callout_number").val();
        } 
        if($("#task_name").val()!="") {
            url += "&task_name="+ encodeURI($("#task_name").val());
        }        
        if(f.user_name && $("#user_name").val()!="") {
            url += "&user_name="+ $("#user_name").val();
        }  
		$('#flex1').flexOptions({'url': url}).flexReload(); 
	}

</script>
</body>
</html>
